<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Plugins_OfflineMessage extends Gio_Core_Plugin
{
	public function run()
	{	
		$request = Gio_Core_Request::getInstance();
		$configs = Gio_Core_Config_Xml::getConfig('web');
		$adminPrefixUrl = $configs->admin->url_prefix;
		
		$baseUrl = $request->getBaseUrl();
		$uri = $request->getRequestUri();
		$serverName = $request->getServerName();
		$serverPort = $request->getServerPort();
		$serverPort = ($serverPort == 80) ? '' : ':' . $serverPort;
		$uri = 'http://' . $serverName . $serverPort . $uri;
		$currentUrl = str_replace($baseUrl, '', $uri);
		$currentUrl = '/' . rtrim(ltrim($currentUrl, '/'), '/') . '/';
		
		/**
		 * 7 is length of /admin/
		 */
		$length = strlen($adminPrefixUrl) + 2;
		if (!$adminPrefixUrl || substr($currentUrl, 0, $length) != "/$adminPrefixUrl/") {
			if (isset($configs->offline->enable) && (string)$configs->offline->enable == 'true') {
				$message = isset($configs->offline->message) ? $configs->offline->message : null;
				Modules_Core_Services_Exception::error('OFFLINE_MESSAGE');
			}
		}
	}
}